//
//  SRSettingClockView.m
//  SportRing
//
//  Created by Elwin on 14-11-5.
//  Copyright (c) 2014年 HotOne. All rights reserved.
//

#import "SRSettingClockView.h"
#import "NSDate+Additionals.h"

@interface SRSettingClockView()
{
    NSInteger totalRow;
}

@property (nonatomic, weak)IBOutlet UITableView *srSRSettingTableView;

@property (nonatomic, weak)IBOutlet UIDatePicker *timePicker;

@property (nonatomic, weak)IBOutlet UIView *chooseWeekDayContainerView;

@property (nonatomic, strong)NSArray *selectedWeekDays;

@property (nonatomic, strong)NSArray *dataSourceArr;

@property (nonatomic, assign)CLOCK_RING_TYPE ringType;

@property (nonatomic, strong)__block SRSilenceClockTableViewCellModel *changedClockModel;



@end

@implementation SRSettingClockView

- (void)addTimePickerWithTime:(NSDate *)date
{
    _timePicker.datePickerMode = UIDatePickerModeTime;
    
    _timePicker.date = date;
    
    [_timePicker addTarget:self action:@selector(selectedTime:) forControlEvents:UIControlEventValueChanged];
}

- (void)awakeFromNib
{
    [super awakeFromNib];
    
    totalRow = 4;

    [self addTimePickerWithTime:[NSDate date]];
    
    
}

- (void)setClockModel:(SRSilenceClockTableViewCellModel *)clockModel
{
    if (_clockModel == clockModel)
    {
        return;
    }
    
    _clockModel = clockModel;
    
    _changedClockModel = [_clockModel copy];

    
    [self setPikcerTime];
    
    [self chooseAlarmType];
    _ringType = CLOCK_RING_TYPE_CUSTOM;
    
    _changedClockModel.ringType = _ringType;
    
    [self setMultiSelectWeekDay:YES];
}

- (void)setPikcerTime
{
    NSDate *date = [NSDate dateFromString:_clockModel.time format:@"HH:mm"];
    _timePicker.date = date;
}

- (void)chooseAlarmType
{
    //默认的闹钟，主动选中每一天
    if (_clockModel.ringType == CLOCK_RING_TYPE_DEFAULT)
    {
        _clockModel.ringType = CLOCK_RING_TYPE_ERERY_DAY;
        _clockModel.monday = @1;
        _clockModel.tuesday = @1;
        _clockModel.wednesday = @1;
        _clockModel.thursday = @1;
        _clockModel.friday = @1;
        _clockModel.saturday = @1;
        _clockModel.sunday = @1;
    }
    if (_clockModel.ringType == CLOCK_RING_TYPE_ONE_TIME)
    {
        [self tableView:_srSRSettingTableView didSelectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
        [_srSRSettingTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:YES scrollPosition:UITableViewScrollPositionBottom];
    }
    else if (_clockModel.ringType == CLOCK_RING_TYPE_ERERY_DAY)
    {
        [self tableView:_srSRSettingTableView didSelectRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0]];
        [_srSRSettingTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0] animated:YES scrollPosition:UITableViewScrollPositionBottom];
    }
    else if (_clockModel.ringType == CLOCK_RING_TYPE_MONDAY_TO_FRIDAY)
    {
        [self tableView:_srSRSettingTableView didSelectRowAtIndexPath:[NSIndexPath indexPathForRow:2 inSection:0]];
        [_srSRSettingTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:2 inSection:0] animated:YES scrollPosition:UITableViewScrollPositionBottom];
    }
    else if (_clockModel.ringType == CLOCK_RING_TYPE_CUSTOM)
    {
        [_srSRSettingTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:3 inSection:0] animated:YES scrollPosition:UITableViewScrollPositionBottom];
        
        [self tableView:_srSRSettingTableView didSelectRowAtIndexPath:[NSIndexPath indexPathForRow:3 inSection:0]];
        
    }
}

- (NSString *)stringFromArr:(NSArray *)arr
{
    NSMutableString *content = [[NSMutableString alloc] init];
    
    [arr enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        [content appendString:[NSString stringWithFormat:@"%@  ",(NSString *)obj]];
    }];
    
    return content;
}

#pragma mark - UITableViewDataSource ,UITableViewDelegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
    
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    return totalRow;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 4)
    {
        return 100;
    }
    return 44;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    SRSettingClockTableViewCell *settingCell = [tableView dequeueReusableCellWithIdentifier:@"SRSettingClockTableViewCell"];
    
    [settingCell selectRow:YES];
    
    if (indexPath.row == 0)
    {
        [settingCell setTitle:@"只响一次"];
        
        if (_clockModel.ringType == CLOCK_RING_TYPE_ONE_TIME)
        {
            [settingCell selectRow:YES];
        }

        
    }
    else if(indexPath.row == 1)
    {
        [settingCell setTitle:@"每天"];
        
        if (_clockModel.ringType == CLOCK_RING_TYPE_ERERY_DAY)
        {
            [settingCell selectRow:YES];
        }
    }
    else if(indexPath.row == 2)
    {
        [settingCell setTitle:@"周一至周五"];
        
        if (_clockModel.ringType == CLOCK_RING_TYPE_MONDAY_TO_FRIDAY)
        {
            [settingCell selectRow:YES];
        }
    }
    else if (indexPath.row == 3)
    {
        [settingCell setTitle:@"自定义"];
        
        if (_clockModel.ringType == CLOCK_RING_TYPE_CUSTOM)
        {
            [settingCell selectRow:YES];
        }
        else
        {
            [settingCell selectRow:NO];

        }
        
        [settingCell setContent:[self stringFromArr:_selectedWeekDays]];

    }
    
    return settingCell;

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (indexPath.row == 3)
    {
        if (_ringType == CLOCK_RING_TYPE_CUSTOM)
        {
            _ringType = CLOCK_RING_TYPE_OFF;
            [self setMultiSelectWeekDay:NO];

        }
        else
        {
            _ringType = CLOCK_RING_TYPE_CUSTOM;
            
            _changedClockModel.ringType = _ringType;
            
            [self setMultiSelectWeekDay:YES];
        }


    }
    else
    {
        [self setMultiSelectWeekDay:NO];
        
        if (indexPath.row == 0)
        {
            _ringType = CLOCK_RING_TYPE_ONE_TIME;
            _changedClockModel.ringType = _ringType;
            
            _changedClockModel.monday = @0;
            _changedClockModel.tuesday = @0;
            _changedClockModel.wednesday = @0;
            _changedClockModel.thursday = @0;
            _changedClockModel.friday = @0;
            _changedClockModel.saturday = @0;
            _changedClockModel.sunday = @0;
            
            int week = [[NSDate date] weekDay];
            //monday
            if (week == 2)
            {
                _changedClockModel.monday = @1;
            }
            else if (week == 3)
            {
                _changedClockModel.tuesday = @1;
            }
            else if (week == 4)
            {
                _changedClockModel.wednesday = @1;
            }
            else if (week == 5)
            {
                _changedClockModel.thursday = @1;
            }
            else if (week == 6)
            {
                _changedClockModel.friday = @1;
            }
            else if (week == 7)
            {
                _changedClockModel.saturday = @1;
            }
            else if (week == 1)
            {
                _changedClockModel.sunday = @1;
            }

        }
        if (indexPath.row == 1)
        {
            _ringType = CLOCK_RING_TYPE_ERERY_DAY;
            _changedClockModel.ringType = _ringType;
            _changedClockModel.monday = @1;
            _changedClockModel.tuesday = @1;
            _changedClockModel.wednesday = @1;
            _changedClockModel.thursday = @1;
            _changedClockModel.friday = @1;
            _changedClockModel.saturday = @1;
            _changedClockModel.sunday = @1;
        }
        if (indexPath.row == 2)
        {
            _ringType = CLOCK_RING_TYPE_MONDAY_TO_FRIDAY;
            _changedClockModel.ringType = _ringType;
            _changedClockModel.monday = @1;
            _changedClockModel.tuesday = @1;
            _changedClockModel.wednesday = @1;
            _changedClockModel.thursday = @1;
            _changedClockModel.friday = @1;
            _changedClockModel.saturday = @0;
            _changedClockModel.sunday = @0;
            
        }
    }
    
    if (_settingClockDelegate && [_settingClockDelegate respondsToSelector:@selector(changedClockModel:orginColckModel:)])
    {
        [_settingClockDelegate changedClockModel:_changedClockModel orginColckModel:_clockModel];
    }
    
}


- (void)setMultiSelectWeekDay:(BOOL)visiabale
{
    ELMultiSelectView *elMultiSelectiWeekDayView;
    elMultiSelectiWeekDayView.layer.cornerRadius = 10;
    if (visiabale)
    {
        elMultiSelectiWeekDayView = [[ELMultiSelectView alloc] initWithFrame:self.chooseWeekDayContainerView.frame];
        
        elMultiSelectiWeekDayView.delegate = self;
        
        elMultiSelectiWeekDayView.dataSource = self;
        
        elMultiSelectiWeekDayView.backgroundColor = [SetFontColorProperty setFontColor:97 float2:97 float3:97];
        
        self.chooseWeekDayContainerView.backgroundColor = elMultiSelectiWeekDayView.backgroundColor;
        
        [elMultiSelectiWeekDayView setItemsCountPerLine:7];
        
        [self.chooseWeekDayContainerView addSubview:elMultiSelectiWeekDayView];
        
        elMultiSelectiWeekDayView.center = self.chooseWeekDayContainerView.center;
        
        
        _dataSourceArr = [NSArray arrayWithObjects:@"日",@"一",@"二",@"三",@"四",@"五",@"六", nil];
        

        elMultiSelectiWeekDayView.selecteItemsIndexArr = [SRSilenceClockTableViewCellModel selectedWeekDays:_clockModel];

        [self didClickOneItemOnSelectView:elMultiSelectiWeekDayView];

    }
    else
    {
        self.chooseWeekDayContainerView.backgroundColor = [UIColor clearColor];

        for (id subview in self.chooseWeekDayContainerView.subviews)
        {
            if ([subview isKindOfClass:[ELMultiSelectView class]])
            {
                [subview removeFromSuperview];
            }
        }
    }

}



- (void)selectedTime:(id)sender
{
    UIDatePicker *picker = (UIDatePicker *)sender;
    
    _changedClockModel.time = [picker.date formattedBy_hhmm];
    
    if (_settingClockDelegate && [_settingClockDelegate respondsToSelector:@selector(changedClockModel:orginColckModel:)])
    {
        [_settingClockDelegate changedClockModel:_changedClockModel orginColckModel:_clockModel];
    }
    
}


- (void)customAlarm:(NSArray *)selecteItemsIndexArr
{
    if ([selecteItemsIndexArr containsObject:@1])
    {
        _changedClockModel.monday = @1;
        
    }
    else
    {
        _changedClockModel.monday = @0;
        
    }
    
    
    if ([selecteItemsIndexArr containsObject:@2])
    {
        _changedClockModel.tuesday = @1;
        
    }
    else
    {
        _changedClockModel.tuesday = @0;
        
    }
    
    
    
    if ([selecteItemsIndexArr containsObject:@3])
    {
        _changedClockModel.wednesday = @1;
        
    }
    else
    {
        _changedClockModel.wednesday = @0;
        
    }
    
    if ([selecteItemsIndexArr containsObject:@4])
    {
        _changedClockModel.thursday = @1;
        
    }
    else
    {
        _changedClockModel.thursday = @0;
        
    }
    
    if ([selecteItemsIndexArr containsObject:@5])
    {
        _changedClockModel.friday = @1;
        
    }
    else
    {
        _changedClockModel.friday = @0;
        
    }
    
    if ([selecteItemsIndexArr containsObject:@6])
    {
        _changedClockModel.saturday = @1;
        
    }
    else
    {
        _changedClockModel.saturday = @0;
        
    }
    
    if ([selecteItemsIndexArr containsObject:@0])
    {
        _changedClockModel.sunday = @1;
        
    }
    else
    {
        _changedClockModel.sunday = @0;
        
    }
}



#pragma mark - MultiSelecteView delegate

- (void)didClickOneItemOnSelectView:(ELMultiSelectView *)multiSelectView{

    if (_settingClockDelegate && [_settingClockDelegate respondsToSelector:@selector(changedClockModel:orginColckModel:)])
    {
        __block NSMutableArray *selectedArr = [[NSMutableArray alloc] init];
        
        [multiSelectView.selecteItemsIndexArr enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
            
            [selectedArr addObject:[_dataSourceArr objectAtIndex:[obj integerValue]]];

        }];
        
        [self customAlarm:multiSelectView.selecteItemsIndexArr];
        
        _selectedWeekDays = selectedArr;
                
        [_srSRSettingTableView reloadData];
        
        [_srSRSettingTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:3 inSection:0] animated:YES scrollPosition:UITableViewScrollPositionBottom];
        
        [_settingClockDelegate changedClockModel:_changedClockModel orginColckModel:_clockModel];
    }
}


#pragma mark - MultiSelecteView dataSource

- (NSInteger)itemsCountForMultiSelectView:(ELMultiSelectView *)multiSelectView{
    
    return [_dataSourceArr count];
}

- (ELSelectView *)multiSelectView:(ELMultiSelectView *)multiSelectView viewForItemAtIndex:(NSInteger)index{
    
    ELSelectView *selectView = [[ELSelectView alloc] initWithTitle:[_dataSourceArr objectAtIndex:index]];
    
    return selectView;
}





@end
